﻿<?php

/**
 * @author anhtung
 * @copyright 2013
 */

class Hoso extends CI_Controller{
    
    protected $mahoso;
    
    function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('userlayout');
        $this->userlayout->setLayout("user/layout");
        
        $userSession = $this->session->userdata('user');
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
        }
        $this->mahoso = $userSession['MaHoSo'];
    }
    
    
    function getDetail($id = null){
        if($id == null){
            $staffId = $this->mahoso;    
        }
        else{
            $staffId = $id;
        }
        if($staffId !== false){
            $this->load->model("user/hoso_model");
            $this->load->model("user/work_model");
            $this->load->model("user/position_model");
            $this->load->model("user/contract_model");
            $this->load->model("user/bophan_model");
            $result = $this->hoso_model->getDetailStaff($staffId);
            $maCV = $this->work_model->getById($result[0]->MaCongViec);
            $maCD = $this->position_model->getById($result[0]->MaChucDanh);
            $maHD = $this->contract_model->getById($result[0]->MaLoaiHopDong);
            $maBP = $this->bophan_model->getById($result[0]->MaBoPhan);
            if($maCV != false){
                $result[0]->TenCongViec = $maCV[0]->TenCongViec;    
            }
            
            if($maCD != false){
                $result[0]->TenChucDanh = $maCD[0]->TenChucDanh;   
            }
            
            if($maHD != false){
                $result[0]->TenLoaiHopDong = $maHD[0]->TenLoaiHopDong;    
            }
            
            if($maBP != false){
                $result[0]->TenBoPhan = $maBP[0]->TenBoPhan;    
            }
            $data['data'] = $result[0];
            $data['mahoso'] = $this->mahoso;
            $title = $result[0]->HoTen;
            $currTemp = array('&nbsp;&nbsp; Thông tin cá nhân');
            $current['current'] = $currTemp;
            log_message('debug', "Model Class Initialized");
            $this->userlayout->view("user/chitiethoso",$data ,$title, $current);
            
        }
        
    }
    
    function doupload(){
        if(isset($_FILES['img-profile']['tmp_name']) && $_FILES['img-profile']['tmp_name'] != null && $_FILES['img-profile']['error'] = 0 ){
        $uploaddir = APPROOT.'resources/hoso/';
        $path = $_FILES['img-profile']['name'];
        $ext = pathinfo($path, PATHINFO_EXTENSION);
        $file_name = "img_hoso_".$this->mahoso.".".$ext;
        $file_path = $uploaddir.$file_name;
        if(move_uploaded_file($_FILES['img-profile']['tmp_name'],$file_path)){
                $this->load->model("user/hoso_model");
                $data['MaHoSo'] = $this->mahoso;
                $data['AnhDaiDien'] = $file_name;
                $result = $this->hoso_model->updateStaff($data);
                redirect('nhanvien/hoso/getDetail');
        }
        }else{
            redirect('nhanvien/hoso/getDetail');
        }
    }
    
    function danhsachnhanvien(){
        $currTemp = array('&nbsp; &nbsp;Danh sách nhân viên');
        $current['current'] = $currTemp;
        $title = "Danh sách nhân viên";
        $this->userlayout->view("user/list_nhan_vien",null ,$title, $current);
        
    }
    
    function getList(){
        $data['sort'] = $_GET["jtSorting"];
        $data['startIndex'] = $_GET["jtStartIndex"];
        $data['pageSize'] = $_GET["jtPageSize"];
        $this->load->model("user/hoso_model");
        $result = $this->hoso_model->getList($data);
        $jTableResult = array();
		$jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $result['recordCount'];
		$jTableResult['Records'] = $result['query'];
		print json_encode($jTableResult);
    }
    
    function congViec(){
        //$id = $_GET['id'];
        $this->load->model("admin/work_model");
        $result = $this->work_model->getAll();
        $jTableResult = array();
        $rows = array();
        foreach($result as $row)
        {
            $rows[]=array("Value"=>$row->MaCongViec,"DisplayText"=>$row->TenCongViec);

        }

		$jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $rows;
        print json_encode($jTableResult);
    }
    
    
    function chucDanh(){
        //$id = $_GET['id'];
        $this->load->model("admin/position_model");
        $result = $this->position_model->getAll();
        $jTableResult = array();
        $rows = array();
        foreach($result as $row)
        {
            $rows[]=array("Value"=>$row->MaChucDanh,"DisplayText"=>$row->TenChucDanh);

        }

		$jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $rows;
        print json_encode($jTableResult);
    }
    
}

?>